<?php
/**
 * Author: yixuan
 * Desc:member操作函数
 * require_path: 挂载文件路径  application/common.php:17
 * Time: 2016/5/18 17:31
 */

use think\Db;


/**
 * Desc:获取member信息
 * User: yixuan
 * Time: 2016/5/27 10:37
 * @param $uid_openid
 * @param $unionid
 * @return array
 */
if (!function_exists('member_info')) {
    function member_info($uid_openid)
    {
        global $_G;
        if (empty($uid_openid)) {
            return [];
        }
        if (is_numeric($uid_openid)) {
            $where = ' uid=' . $uid_openid;
        } else {
            $where = " openid ='" . $uid_openid . "'";
        }
        $member = Db::table('yx_sys_members')
            ->where($where)
            ->find();
        if (!$member) {
            return [];
        }
        if (empty($member['nickname'])) {
            $member['nickname'] = !empty($member['realname']) ? $member['realname'] : '空';
        }
        $_G['uid'] = $member['uid'];
        unset($member['unionid']);
        $_G['member'] = !empty($_G['member']) ? array_merge($_G['member'], $member) : $member;
        $_G['member']['username'] = $member['realname'];
        return $member;
    }

}
/**
 * Desc:member的添加和更改
 * User: yixuan
 * Time: 2016/5/27 10:40
 * @param $data
 * @param $uid_openid
 * @param $unionid
 * @return false|int|string
 */
if (!function_exists('member_save')) {
    function member_save($data, $uid_openid = '')
    {
        if (empty($data)) {
            return false;
        }
        if ($uid_openid) {
            $where = '';
            if (is_numeric($uid_openid)) {
                $where .= " uid ='" . $uid_openid . "'";
            } else {
                $where .= "  openid ='" . $uid_openid . "'";
            }
            $result = Db::table('yx_sys_members')->where($where)->update($data);
            return member_info($uid_openid);
        }
        if (empty($data['openid'])) {
            return false;
        }
        $memberid = Db::table('yx_sys_members')->insertGetId($data);
        return member_info($memberid);
    }
}

/**
 * Desc:member更改
 * User: yixuan
 * Time: 2016/5/27 10:40
 * @param $data
 * @param $uid_openid
 * @param $unionid
 * @return false|int|string
 */
if (!function_exists('member_update')) {
    function member_update($uid_openid, $data)
    {
        if (empty($data)) {
            return false;
        }
        if (is_numeric($uid_openid)) {
            $where = ' uid =' . $uid_openid;
        } else {
            $where = "  openid ='" . $uid_openid . "'";
        }
        $result = Db::table('yx_sys_members')->where($where)->update($data);
        return member_info($uid_openid);
    }
}